let url = 'http://ab.boatim.top'


export const get = (api,text,isToken=false) => {
    return new Promise((r, e) => {
        wx.showLoading({
            title: text,
            mask: true
        })
        wx.request({
            url: url + api, // 仅为示例，并非真实的接口地址
            method: 'GET',
            header: {
                'content-type': 'application/json', // 默认值
                'Authorization':isToken?`Bearer ${wx.getStorageSync('ctToken')}`:''
            },
            success(res) {
                r(res)
            },
            fail(res) {
                e(res)
            }
        })
    })
}

export const post = (api, obj, text, isToken=false) => {
    return new Promise((r, e) => {
        wx.showLoading({
            title: text,
            mask: true
        })
        wx.request({
            url: url + api, // 仅为示例，并非真实的接口地址
            data: obj,
            method: 'POST',
            header: {
                'content-type': 'application/json', // 默认值
                'Authorization':isToken?`Bearer ${wx.getStorageSync('ctToken')}`:''
            },
            success(res) {
                r(res)
            },
            fail(res) {
                e(res)
            }
        })
    })
}

export const put = (api, obj, text, isToken=false) => {
    return new Promise((r, e) => {
        wx.showLoading({
            title: text,
            mask: true
        })
        wx.request({
            url: url + api, // 仅为示例，并非真实的接口地址
            data: obj,
            method: 'PUT',
            header: {
                'content-type': 'application/json', // 默认值
                'Authorization':isToken?`Bearer ${wx.getStorageSync('ctToken')}`:''
                // 'Authorization': 'Bearer ' +2232  // 默认值
            },
            success(res) {
                r(res)
            },
            fail(res) {
                e(res)
            }
        })
    })
}




export const _delete = (api, obj, text) => {
    return new Promise((r, e) => {
        wx.showLoading({
            title: text,
            mask: true
        })
        wx.request({
            url: url + api, // 仅为示例，并非真实的接口地址
            data: obj,
            method: 'DELETE',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                r(res)
                console.log(res)
            },
            fail(res) {
                e(res)
            }
        })
    })
}

export const postTran = (api, obj)=>{
    return new Promise((r, e) => {
        wx.request({
            url: url + api, // 仅为示例，并非真实的接口地址
            data: obj,
            method: 'POST',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                r(res)
            },
            fail(res) {
                e(res)
            }
        })
    })
}


// 支付
export const paly = (obj) => {
    return new Promise((r, j) => {
        wx.showLoading({
            title: '创建订单..',
            mask: true
        })
        wx.request({
            url: url + '/pay', // 仅为示例，并非真实的接口地址
            data: obj,
            method: 'POST',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(pay) {
                console.log('签名', pay)
                wx.requestPayment({
                    timeStamp: pay.data.timeStamp,
                    nonceStr: pay.data.nonceStr,
                    package: pay.data.package,
                    signType: 'MD5',
                    paySign: pay.data.paySign,
                    success(res) {
                        console.log(res)
                        r(res)
                        // wx.hideLoading()
                    },
                    fail(res) {
                        console.log('支付失败', res)
                        j(res)
                        // wx.hideLoading()
                        if (res.errMsg = "requestPayment:fail cancel") {
                            wx.showToast({
                                title: '未支付',
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    }
                })
            },
            fail(res) {
                console.log('支付错误', res)
                // wx.hideLoading()
            }
        })
    })

}

// 上传文件  文件，位置，名称
export const upFile = (path, keys, text) => {
    return new Promise((r, e) => {
        wx.uploadFile({
            url: 'https://imh2.oss-cn-huhehaote.aliyuncs.com', //仅为示例，非真实的接口地址
            filePath: path,
            name: 'file',
            formData: {
                name: path,
                key: keys,
                policy: "eyJleHBpcmF0aW9uIjoiMjAyMi0wOS0wMVQxMjowMDowMC4wMDBaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF1dfQ==",
                OSSAccessKeyId: 'LTAI4FwYoSicidUTkUawww2m',
                signature: 'GVpt/rWKd1Wy9Jk/mumfkUfNkW8=',
                success_action_status: "200"
            },
            success: function (res) {
                var data = res.data
                console.log('上传成功', res)
                r({url:'https://imh2.oss-cn-huhehaote.aliyuncs.com/'+keys})
                // wx.hideLoading()
            },
            fail(err) {
                j(err)
                // wx.hideLoading()
            }
        })
    })
}

